﻿#ifndef CHANNELINFO_H
#define CHANNELINFO_H

#include <QVariantHash>
#include <QVector>

enum msg_extra_define
{
    msg_dir_send = 0,
    msg_dir_recv = 1,

    msg_type_dispatch_req = 7,
    msg_type_dispatch_req_end = 8,
    msg_type_dispatch_end = 9,
    msg_type_ask_online = 11,
    msg_type_ask_online_ack = 12,
    msg_type_notify = 15,
    msg_type_dispatch_reject = 16,
    msg_type_dispatch_start = 17,
    msg_type_msg_readed = 18,
    msg_type_msg_ask_readed = 19,
    msg_type_dispatch_switch_camera = 20,
    msg_type_dispatch_turn_left = 21,
    msg_type_dispatch_turn_right = 22,
    msg_type_share_video = 30,

    msg_state_send = 0,
    msg_state_send_succ = 1,
    msg_state_send_fail = 2,
    msg_state_peer_read = 3,
    msg_state_recv = 0,
    msg_state_readed = 1
};

class BaseChannelInfo
{
public:
    BaseChannelInfo();
    virtual ~BaseChannelInfo() {}
    quintptr internalId() const;
    static const BaseChannelInfo *fromInternalId(quintptr iId);

    qint64 uniqueId() const;
    static qint64 idFromUniqueId(const qint64 &unique);
    static bool isP2PFromUniqueId(const qint64 &unique);
    static QPair<bool, qint64> fromUniqueId(const qint64 &unique);
    static qint64 makeUniqueId(const qint64 &id, bool isP2P);
    static bool equals(const qint64 &unique, const qint64 &id, bool isP2P);

    virtual bool isP2P() const = 0;
    virtual qint64 id() const = 0;
    virtual qint64 getRid() const = 0;
    virtual QString display() const = 0;
    virtual QVariant decoration() const = 0;
    virtual void call_mark_read(const QByteArray &mid) const = 0;

    QVariantHash &create_msg(qint64 msg_time, const QByteArray &mid);
    bool add_msg_list(const QByteArray &mid, QVariantHash &msg, bool isRead);
    QByteArrayList mark_read(const QByteArray &mid = QByteArray());
    bool mark_peer_read(const QByteArray &mid);
    QList<QVariantHash> get_msg_list() const;
    void clear_msg_list();
    qint64 last_msg_time() const;
    QString last_msg() const;
    QString getMemberName(qint64 uid) const;
    static bool msg_less_than(const QVariantHash &left, const QVariantHash &right);
    static bool isVisibleMsg(const QVariantHash &msg, bool inChat);
    static bool isVisibleMsg(const int type, bool inChat);
    static bool isUnreadMsg(const QVariantHash &msg);
    static bool isUnreadMsg(const int type, const QVariantHash &msg);
    bool mute;
    QList<QVariantHash> member_list;
    quint32 unread_count;
private:
    QMultiMap<qint64, QVariantHash *> msg_index;
    QHash<QByteArray, QVariantHash> msg_list;
};

class ChannelInfo : public BaseChannelInfo
{
public:
    ChannelInfo(qint64 id = 0);
    ChannelInfo(qint64 id, QVariantHash &info);
    bool isP2P() const Q_DECL_OVERRIDE;
    qint64 id() const Q_DECL_OVERRIDE;
    qint64 getRid() const Q_DECL_OVERRIDE;
    QString display() const Q_DECL_OVERRIDE;
    QVariant decoration() const Q_DECL_OVERRIDE;
    void call_mark_read(const QByteArray &mid) const Q_DECL_OVERRIDE;
    static void call_mark_read_all(qint64 rid, const QByteArrayList &mids);
    qint64 rid;
    int rstyle;
    QString rname;

    QVector<QSet<qint64>> mic_changes;

    QVariantHash room_info;
    QList<QVariantHash> mic_list;
    QList<QVariantHash> apply_list;

};

class P2PChanneInfo : public BaseChannelInfo
{
public:
    bool isP2P() const Q_DECL_OVERRIDE;
    qint64 id() const Q_DECL_OVERRIDE;
    qint64 getRid() const Q_DECL_OVERRIDE;
    QString display() const Q_DECL_OVERRIDE;
    QVariant decoration() const Q_DECL_OVERRIDE;
    void call_mark_read(const QByteArray &mid) const Q_DECL_OVERRIDE;
    static void call_mark_read_all(qint64 uid, const QByteArrayList &mids);
    static const qint64 p2pchannel_room_id = 41999999;
    static const int p2pchannel_audio_id = 10;
    static const int p2pchannel_video_id = 10;
    static const QString start_talk;
    static const QString end_talk;
    qint64 uid;
    QString name;
    QVariant icon;
    bool hasVideo;
    bool hasAudio;
};

#endif // CHANNELINFO_H
